package com.bianguo.android.apushdemo.view.activity;

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bianguo.android.apushdemo.R;
import com.bianguo.android.apushdemo.adapter.SuspensionAdapter;
import com.bianguo.android.apushdemo.entity.SuspensionData;
import com.bianguo.android.apushdemo.utils.MyLog;

import java.util.ArrayList;

import butterknife.BindView;

public class SuspensionActivity extends BaseActivity {

    @BindView(R.id.suspension_recycler)
    RecyclerView mRecycler;
    @BindView(R.id.suspension_tv)
    TextView suspensionTv;

    private SuspensionAdapter adapter;
    private ArrayList<SuspensionData> suspensionDatas;
    private LinearLayoutManager layoutManager;

    @Override
    public int layoutId() {
        return R.layout.activity_suspension;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        suspensionDatas = new ArrayList<>();
        for (int i = 0, j = 0; i < 60; i++) {
            SuspensionData data = new SuspensionData();
            if (i > 0 && i % 10 == 0) {
                data.setShowGroup(true);
                j++;
            }
            data.setContent("content " + i);
            data.setGroup(j);
            suspensionDatas.add(data);
        }
        layoutManager = new LinearLayoutManager(mContext);
        mRecycler.setLayoutManager(layoutManager);
        adapter = new SuspensionAdapter(mContext, suspensionDatas);
        mRecycler.setAdapter(adapter);
        mRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                View view = recyclerView.findChildViewUnder(suspensionTv.getMeasuredWidth() / 2, 5);
                if (view != null) {
                    TextView tv = (TextView) view.findViewById(R.id.item_suspension_group);
                    if (tv != null) {
                        suspensionTv.setText(tv.getText().toString());
                    }
                }

                View suspensionView = recyclerView.findChildViewUnder(suspensionTv.getMeasuredWidth() / 2, suspensionTv.getMeasuredHeight());
                if (suspensionView != null) {
                    TextView tv = (TextView) suspensionView.findViewById(R.id.item_suspension_group);
                    if (tv != null && View.VISIBLE == tv.getVisibility()) {
                        int dealY = suspensionView.getTop() - suspensionTv.getMeasuredHeight();
                        if (suspensionView.getTop() > 0) {
                            suspensionTv.setTranslationY(dealY);
                        } else {
                            suspensionTv.setTranslationY(0);
                        }
                    }
                }
                int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
                if (firstVisibleItemPosition <= 0){
                    MyLog.mLog("suspensionView == null");
                    suspensionTv.setTranslationY(0);
                }
            }
        });
    }
}
